/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import model.TrangThai;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author MRDO
 */
public class TrangThaiDao {
    private static Session session;
    
    public TrangThaiDao(){
        SessionFactory factory = HibernateUtil.getSessionFactory();
        this.session = factory.getCurrentSession();
    }
    
    /*
     * insertTrangThai
     */
    public boolean insertTrangThai(String tenTrangThai){
        try {
            session.beginTransaction();
            
            TrangThai trangThai = new TrangThai(tenTrangThai);
            
            session.save(trangThai);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return false;
    }
    
    /*
     * updateTrangThai
     */
    public boolean updateTrangThai(int id, String tenTrangThai){
        try {
            session.beginTransaction();
            
            TrangThai trangThai = (TrangThai) session.get(TrangThai.class, id);
            trangThai.setTenTrangThai(tenTrangThai);
            
            session.update(trangThai);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return  false;
    }
    
    /*
     * deleteTrangThai
     */
    public boolean deleteTrangThai(int id){
        try {
            session.beginTransaction();
            
            TrangThai trangThai = (TrangThai) session.get(TrangThai.class, id);
            
            session.delete(trangThai);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
        }
        return  false;
    }
}
